package com.example.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.Service.PurchaseOrderService;
import com.example.dao.PurchaseOrderMapper;
import com.example.pojo.Announcement;
import com.example.pojo.PurchaseOrder;
import com.example.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
//@RequestMapping("/purchaseOrder")
@Api(tags = "采购单接口")
public class PurchaseOrderController{
    @Resource
    private PurchaseOrderService purchaseOrderService;
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;


    @PostMapping("/selectAllByReview")
    public Object selectAllByReview(){//查询待审批
        QueryWrapper<PurchaseOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("review","待审核");
        List<PurchaseOrder> purchaseOrders=purchaseOrderService.list(queryWrapper);
        return new Result("200","查询采购",purchaseOrders);
    }
    @PostMapping("/selectNumByReview")
    public Object selectNumByReview(){//查询待审批数量
        QueryWrapper<PurchaseOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("review", "待审核");
        queryWrapper.groupBy("review");
        queryWrapper.select(new String[]{"review", "COUNT(*) as count"}); // 修正为数组形式
        return new Result("200", "查询", purchaseOrderMapper.selectList(queryWrapper));
    }
    @RequestMapping ("/updatePurchase")
    public Object updatePurchase(@RequestBody PurchaseOrder purchaseOrder){//修改审批
        if (purchaseOrder!=null){
            System.out.println(purchaseOrder);
            System.out.println(666);
        }
        if (purchaseOrder.getId() == null) {
            return new Result("400", "ID cannot be null", null);
        }
        return new Result("200","更改采购",purchaseOrderMapper.updateById(purchaseOrder));
    }
    //----------------------楚河------------------------//


    //----------------------汉界------------------------//
    //查询已审核的采购单
    @GetMapping("/getPurchase")
    @ApiOperation("查询已审核的采购单")
    public Object getPurchaseOrderListByReview(@RequestParam(value = "proName",defaultValue = "") String proName,
                                               @RequestParam(value = "page",defaultValue = "1") Integer page,
                                               @RequestParam(value = "size",defaultValue = "5") Integer size){    //查询已审核的采购单
        HashMap<String, Object> map = new HashMap<>();
        QueryWrapper<PurchaseOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("review","已通过").orderByDesc("create_time");
        map.put("purchaseOrderList",purchaseOrderService.list(queryWrapper));
        queryWrapper.like("pro_name",proName);
        Page<PurchaseOrder> Page= new Page<>(page,size);
        Page<PurchaseOrder> purchaseOrderPage= purchaseOrderService.page(Page,queryWrapper);
        for (PurchaseOrder purchaseOrder : purchaseOrderPage.getRecords()) {
            purchaseOrder.setTotal(purchaseOrder.getPrice()*purchaseOrder.getQuantity());
        }
        map.put("total",purchaseOrderPage.getTotal());
        map.put("rows",purchaseOrderPage.getRecords());
        map.put("page",purchaseOrderPage.getPages());
        map.put("current",purchaseOrderPage.getCurrent());
        map.put("size",purchaseOrderPage.getSize());
        return new Result<>("200","查询成功",map);
    }


    @GetMapping("updatePurchaseOrder")
    @ApiOperation("批量购买")
    public Object updatePurchaseOrderListByReviewAndList(String ids) {
        List<String> ids2 = Arrays.stream(ids.split(",")).toList();
        boolean isOk = purchaseOrderService.lambdaUpdate().in(PurchaseOrder::getId,ids2).
                set(PurchaseOrder::getDelivered,"已购买").update();
        if(isOk) return new Result<>("200","购买成功",null);
        else return new Result<>("400","购买失败",null);
    }

    @GetMapping("updatePurchase")
    @ApiOperation("修改采购单")
    public Object updatePurchaseOrderListByReviewAndList(Integer id) {
        boolean isOk = purchaseOrderService.lambdaUpdate().eq(PurchaseOrder::getId,id).
                set(PurchaseOrder::getDelivered,"已购买").update();
        if(isOk) return new Result<>("200","修改成功",null);
        else return new Result<>("400","修改失败",null);
    }

}
